package com.lavegue.webaction;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.xml.registry.infomodel.User;

import org.apache.commons.collections.map.HashedMap;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.springframework.web.HttpRequestHandler;

import com.lavegue.common.Constants;
import com.lavegue.common.ListAction;
import com.lavegue.common.Page;
import com.lavegue.model.Area;
import com.lavegue.model.Company;
import com.lavegue.model.TopUp;
import com.lavegue.model.UserInfo;
import com.lavegue.service.AreaService;
import com.lavegue.service.CompanyService;
import com.lavegue.service.TopUpService;
import com.lavegue.service.UserInfoService;
import com.lavegue.util.StringUtil;
import com.lavegue.util.TopupUtil;
import com.opensymphony.xwork2.ActionContext;

public class BackStageUserManage extends ListAction<UserInfo>{

	private UserInfo user;
	private UserInfoService userInfoService;
	private String actionError=""; // 界面显示的错误信息
	private CompanyService companyService;
	private AreaService areaService;
	private String beginTime;   //-->选择开始时间
	private String endTime;     //-->选择结束时间
	private TopUpService topUpService;
	private Page<TopUp> pageTopUpObj; //用户充值记录分页对象
	private String currentPageNo;
	private Integer company_id;  //公司ID
	private String ActionError;
	private String accBalBegin;  //账户开始余额
	private String accBalEnd;  //账户结束余额
	private String xlsFile;    //文件路径
	private String excelPostfixName; //Excel文件的后缀名
	
	// 省列表
	private List<Area> provinces;
	// 城市列表
	private List<Area> cityList;
	// 区域列表
	private List<Area> areaList;
	// 省份ID
	private Integer sf_id;
	// 城市ID
	private Integer cs_id;
	// 区域ID
	private Integer qy_id;
	
	
	public String getExcelPostfixName() {
		return excelPostfixName;
	}
	public void setExcelPostfixName(String excelPostfixName) {
		this.excelPostfixName = excelPostfixName;
	}
	public String getXlsFile() {
		return xlsFile;
	}
	public void setXlsFile(String xlsFile) {
		this.xlsFile = xlsFile;
	}
	public Integer getCompany_id() {
		return company_id;
	}
	public void setCompany_id(Integer companyId) {
		company_id = companyId;
	}
	public String getBeginTime() {
		return beginTime;
	}
	public void setBeginTime(String beginTime) {
		this.beginTime = beginTime;
	}
	public String getEndTime() {
		return endTime;
	}
	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
	public AreaService getAreaService() {
		return areaService;
	}
	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}
	public CompanyService getCompanyService() {
		return companyService;
	}
	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}
	public List<Area> getProvinces() {
		return provinces;
	}
	public void setProvinces(List<Area> provinces) {
		this.provinces = provinces;
	}
	public List<Area> getCityList() {
		return cityList;
	}
	public void setCityList(List<Area> cityList) {
		this.cityList = cityList;
	}
	public List<Area> getAreaList() {
		return areaList;
	}
	public void setAreaList(List<Area> areaList) {
		this.areaList = areaList;
	}
	public Integer getSf_id() {
		return sf_id;
	}
	public void setSf_id(Integer sfId) {
		sf_id = sfId;
	}
	public Integer getCs_id() {
		return cs_id;
	}
	public void setCs_id(Integer csId) {
		cs_id = csId;
	}
	public Integer getQy_id() {
		return qy_id;
	}
	public void setQy_id(Integer qyId) {
		qy_id = qyId;
	}
	
	
	public String getAccBalBegin() {
		return accBalBegin;
	}
	public void setAccBalBegin(String accBalBegin) {
		this.accBalBegin = accBalBegin;
	}
	public String getAccBalEnd() {
		return accBalEnd;
	}
	public void setAccBalEnd(String accBalEnd) {
		this.accBalEnd = accBalEnd;
	}

	public String getCurrentPageNo() {
		return currentPageNo;
	}
	public void setCurrentPageNo(String currentPageNo) {
		this.currentPageNo = currentPageNo;
	}
	public UserInfo getUser() {
		return user;
	}
	public void setUser(UserInfo user) {
		this.user = user;
	}
	public UserInfoService getUserInfoService() {
		return userInfoService;
	}
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
		
	public TopUpService getTopUpService() {
		return topUpService;
	}
	public void setTopUpService(TopUpService topUpService) {
		this.topUpService = topUpService;
	}
	
	public Page<TopUp> getPageTopUpObj() {
		return pageTopUpObj;
	}
	public void setPageTopUpObj(Page<TopUp> pageTopUpObj) {
		this.pageTopUpObj = pageTopUpObj;
	}
	public String getActionError() {
		return actionError;
	}
	public void setActionError(String actionError) {
		this.actionError = actionError;
	}
	/**
	 * ljp 20120104  跳转到进入后台 添加用户信息页面
	 * @return
	 */
	public String toAddUserPage(){
		if(null == this.getSessionAtrribute(Constants.MAN_SESSION_USER))
		{
			return "lg";
		}
		//获取所有的公司名称
		List<Company> comList = companyService.findByCompany();
		// 获取省列表,在前台显示省份
		provinces = areaService.findAreaByParendIdAndlvType(Area.NO_PARENT,
				Area.AreaType.PROVINCE);
		
		setRequestAtrribute("companyList", comList);
		Area area = new Area();
		area.setId(0);
		area.setName("请选择...");
		cityList = new ArrayList<Area>();
		cityList.add(area);
		areaList = new ArrayList<Area>();
		areaList.add(area);
		return "addUserPage";
	}
	
	/**
	 * 20120106 后台操作    添加用户信息功能
	 */
	public String doAddUserInfo(){
		if(null == this.getSessionAtrribute(Constants.MAN_SESSION_USER))
		{
			return "lg";
		}
//		String companyName = user.getCompany().getCompanyName();
		Integer companyID = user.getCompany().getId();
		 //根据公司名称查询
		 Company company = companyService.findCompanyByName(null,companyID);
		 user.setCompany(company);
		 if(null != getSessionAtrribute("htImgIcoPath")){
			 //设置图片路径在页面获取(backStageManage/userInfoAdd.jsp)
			 String personalIco = getSessionAtrribute("htImgIcoPath").toString();
			 if(null != personalIco && !personalIco.equals("")){
				 user.setPersonalIco(personalIco);
			 }
		 }
		// 20120116
		 try {
			 Date date = new Date();
			 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
			 date = sdf.parse(sdf.format(date));
			 user.setAddDate(date); //添加的时间
			 user.setRecentlyLoginDate(date); //最近登录时间
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		 //通过省份ID查询获取名称
	/*	 Area areaProvince = areaService.findById(Integer.valueOf(user.getProvince()));
		 Area areaCity = areaService.findById(Integer.valueOf(user.getCity()));
		 Area areaZone = areaService.findById(Integer.valueOf(user.getZone()));
		 
		 String provinceName = areaProvince.getName();
		 String cityName = areaCity.getName();
		 String zoneName = areaZone.getName();
		 //设置省市区的名称
		 user.setProvince(provinceName);
		 user.setCity(cityName);
		 user.setZone(zoneName);
		 */
		// 20120116
		// user.setBalance(new BigDecimal(0.00));
		 boolean userFalg = userInfoService.userRegisterInfo(user);
		 if(!userFalg){
			actionError = "服务器异常，请联系管理员!";
			
		 }else{
			 TopUp t = new TopUp();
				t.setUserInfo(user); 
				//充值类型,1: 财付通2，银行转账
				t.setTopupType(TopUp.TOPUP_TYPE_WY);
				t.setTopupTime(new Date());
				t.setSum(user.getBalance());
				t.setMsgNum(TopupUtil.getMsgCntByPrice(t.getSum()));
				t.setRemainNum(t.getMsgNum());
				t.setTransactionId("");
				t.setAccRemain(t.getMsgNum());
				topUpService.save(t);
				user.setMsgCnt(t.getAccRemain());
				if (t.getSum().compareTo(new BigDecimal(Constants.VIP)) >=0) {
					user.setType(1);
				}
				userInfoService.update(user);
			 actionError="";
			// 先清除session中user
//			removeSession(Constants.SESSION_USER);
			if(null != getSessionAtrribute("htImgIcoPath")){
				removeSession("htImgIcoPath");
			}
			String account = user.getAccount(); //获取用户号码

			setRequestAtrribute("userAccount", account);
		}
		 return "addUserResult";
	}
	
	/**
	 * @throws Exception 
	 * 20120107 跳转到用户信息展示页面(userInfoSearch.jsp)
	 * @return
	 * @throws  
	 */
	public String toShowUserInfo() throws Exception {
		   if(null==this.getSessionAtrribute(Constants.MAN_SESSION_USER))
		   {
			   return "lg";
		   }
		HttpServletRequest request = ServletActionContext.getRequest();
		//获取所有的公司名称
		List<Company> comList = companyService.findByCompany();
		// 获取省列表,在前台显示省份
		provinces = areaService.findAreaByParendIdAndlvType(Area.NO_PARENT,
				Area.AreaType.PROVINCE);
		
		setRequestAtrribute("companyList", comList);
		//将String转换为Date类型
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
		Date beginDate = null;
		Date endDate = null;
		
		if(null != beginTime && !beginTime.equals("")){
			beginDate = sdf.parse(beginTime);

		}
		if(null != endTime && !endTime.equals("")){
			endDate = sdf.parse(endTime); 
		}
		if(null != getSessionAtrribute("xgCurrentPageNo")){
			removeSession("xgCurrentPageNo");	
		}
		
		//20120203
		if(null != request.getParameter("currentPageNo")){//删除功能使用 -- 获取doDelUserInfoPage()方法请求参数,用来返回当前是第几页
			pageNo = Integer.valueOf(request.getParameter("currentPageNo")).intValue() ; 
		}
		
		if(null != getSessionAtrribute("xgCurrentPageNo")){//修改功能使用 -- 用来返回当前是第几页
			pageNo = Integer.valueOf(getSessionAtrribute("xgCurrentPageNo").toString()).intValue() ; 
		}
		
		if(null != user){
			String account = null;      //账号
			String userName = null;     //用户姓名
			BigDecimal balBegin = null;   //用户余额
			BigDecimal balEnd = null;   //用户余额
			Integer cpNameId = null;   //公司名称ID
			
			Integer userType = null;   //用户属性
			String provinceName = null;  //省份
			String cityName = null;      //城市
			String zoneName = null;      //区县
			String provinceId = null;  //省份ID
			String cityId = null;      //城市ID
			String zoneId = null;      //区县ID
			
			if(null != user.getAccount() && !user.getAccount().equals("")){
				account = user.getAccount(); 
			}
			if(null != user.getUserName() && !user.getUserName().equals("")){
				userName = user.getUserName();
			}
			if(null != accBalBegin && !accBalBegin.equals("")){ //开始余额
				balBegin = new BigDecimal(accBalBegin);
			}
			if(null != accBalEnd && !accBalEnd.equals("")){   //结束余额
				balEnd = new BigDecimal(accBalEnd);
			}
			if(null != user.getCompany().getId() && !user.getCompany().getId().equals("")){
				cpNameId = user.getCompany().getId();
			}
			if(null != user.getType() && !user.getType().toString().equals("")){
				userType = user.getType();
			}
			 //通过省份ID查询获取名称
			//设置省市区的名称ID
			if(null != user.getProvince() && !user.getProvince().equals("")){
			//	Area areaProvince = areaService.findById(Integer.valueOf(user.getProvince()));
				 provinceId = user.getProvince();
			}
			if(null != user.getCity() && !user.getCity().equals("")){
			//	Area areaCity = areaService.findById(Integer.valueOf(user.getCity()));
				cityId = user.getCity();
			}
			if(null != user.getZone() && !user.getZone().equals("")){
			//	Area areaZone = areaService.findById(Integer.valueOf(user.getZone()));
				zoneId = user.getZone();
			}
			 
			try {
				//查询所有的用户数据信息
				pageObj = userInfoService.findUserInfoList(pageNo, pageSize, beginDate, endDate, userName, account, cpNameId, balBegin,balEnd, userType, provinceId, cityId, zoneId);
				List<UserInfo> data = pageObj.getData();
				for (UserInfo info : data) {
					Area p = areaService.findById(Integer.valueOf(info.getProvince()));
					info.setProvince(p.getName()); //设置省份名称
					if (null != info.getCity() && !info.getCity().equals("")) {
						Area c = areaService.findById(Integer.valueOf(info.getCity()));
						info.setCity(c.getName());	 //设置城市名称
					}
					if (null != info.getZone() && !info.getZone().equals("")) {
						Area z = areaService.findById(Integer.valueOf(info.getZone()));
						info.setZone(z.getName());	 //设置区县名称
					} 
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			try {
				//查询所有的用户数据信息
				pageObj = userInfoService.findUserInfoList(pageNo, pageSize, null, null, null, null, null, null, null, null, null, null, null);
				List<UserInfo> data = pageObj.getData();
				for (UserInfo info : data) {
					Area p = areaService.findById(Integer.valueOf(info.getProvince()));
					info.setProvince(p.getName()); //设置省份名称
					if (null != info.getCity() && !info.getCity().equals("")) {
						Area c = areaService.findById(Integer.valueOf(info.getCity()));
						info.setCity(c.getName());	 //设置城市名称
					}
					if (null != info.getZone() && !info.getZone().equals("")) {
						Area z = areaService.findById(Integer.valueOf(info.getZone()));
						info.setZone(z.getName());	 //设置区县名称
					} 
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if(null != getSessionAtrribute("xgUid")){
			removeSession("xgUid");	
		}
		
		if(null != getSessionAtrribute("htImgIcoPath")){
			removeSession("htImgIcoPath");
		}
		if(null != getSessionAtrribute("htErrFileSize")){
			removeSession("htErrFileSize");
		}
		return "toShowUserInfo";
	}

	
	/**
	 * @throws Exception 
	 * 20120109 跳转到后台用户信息展示页面(userInfoList.jsp)
	 * @return
	 * @throws  
	 */
	public String toDisplayAllUserList(){
		  if(null==this.getSessionAtrribute(Constants.MAN_SESSION_USER))
		   {
			   return "lg";
		   }
		try {
			//查询所有的用户信息
			pageObj = userInfoService.queryAllUserList(pageNo, pageSize);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "displayAllUserList";
	}
	
	/**
	 * @throws Exception 
	 * 20120109 跳转到后台用充值记录展示页面(bsRechargeRecordDetail.jsp)
	 * @return
	 * @throws  
	 */
	public String doShowUserRechargeDetail(){
		if(null == this.getSessionAtrribute(Constants.MAN_SESSION_USER))
		{
			return "lg";
		}
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String idString = request.getParameter("userId");  //获取(userInfoSearch.jsp页面)请求参数
		Integer userId = Integer.valueOf(0);
		if(null != idString && !idString.equals("")){
			userId = Integer.valueOf(idString);
		}
		if(null != getSessionAtrribute("userId")){
			removeSession("userId");
		}
		toGetUserinfo(userId);
		//获取当前页数，等到从充值记录页面,返回到用户信息列表当前页面(userInfoSearch.jsp)
		String data = request.getParameter("currentPageNo"); //获取(userInfoSearch.jsp页面)请求参数
		if(null != data && !data.equals("")){
			int currentPageNo = Integer.valueOf(data).intValue();
			request.setAttribute("currentPageNo", currentPageNo);
		}
		
		try {
			//查询所有的用户充值记录信息
			pageTopUpObj = topUpService.findMsgRechargeRecordList(pageNo, pageSize,null,null,userId.intValue());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		setSessionAtrribute("userId", idString);
		
		return "displayUserRechargeDetail";
	}
	
	//20120109 到后台用户充值记录页面查看功能 (bsRechargeRecordDetail.jsp)
	public String toForwordRechargeDetailPage(){
		  if(null==this.getSessionAtrribute(Constants.MAN_SESSION_USER))
		   {
			   return "lg";
		   }
		if(null != currentPageNo && !currentPageNo.equals("")){
			pageNo = Integer.valueOf(currentPageNo).intValue();
		}
		Integer userId = Integer.valueOf(getSessionAtrribute("userId").toString());
		toGetUserinfo(userId);
		try {
			//将String转换为Date类型
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
			Date beginDate = null;
			Date endDate = null;
			
			if(null != beginTime && !beginTime.equals("")){
				beginDate = sdf.parse(beginTime);

			}
			if(null != endTime && !endTime.equals("")){
				endDate = sdf.parse(endTime); 
			}
			//查询所有的用户信息
			pageTopUpObj = topUpService.findMsgRechargeRecordList(pageNo, pageSize,beginDate,endDate,userId.intValue());
						
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "displayUserRechargeDetail";
	}
	
	private void toGetUserinfo(int userid) {
		user = userInfoService.findById(userid);
		Area province = this.areaService.findById(Integer.parseInt(user.getProvince()));
		user.setProvince(province.getName());
		if (!StringUtil.isEmpty(user.getCity())) {
			Area city = this.areaService.findById(Integer.parseInt(user.getCity()));
			user.setCity(city.getName());
		}
		if (!StringUtil.isEmpty(user.getZone())) {
			Area zone = this.areaService.findById(Integer.parseInt(user.getZone()));
			user.setZone(zone.getName());
		}
		List<TopUp> tps = topUpService.findByUser(userid);
		BigDecimal sum = new BigDecimal(0);
		int msgCnt = 0;
		for (TopUp t : tps) {
			sum  = sum.add(t.getSum());
			msgCnt += t.getMsgNum();
		}
		this.setRequestAtrribute("topMoneySum", sum);
		this.setRequestAtrribute("topMsgCnt", msgCnt);
	}
	
	//20120110 跳转到后台用户信息修改页面(bsUserInfoModify.jsp)
	public String toEnterUserInfoModPage(){
		UserInfo userInfo = new UserInfo();
//		UserInfo userInfo = this.getSessionUser(); //假数据测试
		// 判断用户信息是否存在
//		UserInfo user = (UserInfo) getSessionAtrribute(Constants.SESSION_USER);
//		if (null == user) {
//			return "login"; // 不存在跳转到登录页面
//		}
		 if(null==this.getSessionAtrribute(Constants.MAN_SESSION_USER))
		   {
			   return "lg";
		   }
		 
		 HttpServletRequest request = ServletActionContext.getRequest();
		 String idString = request.getParameter("userId");  //获取(userInfoSearch.jsp页面)请求参数
		 String currPageNo = request.getParameter("currentPageNo"); //获取当前页数，等到修改完用户信息返回到当前页
//		 request.setAttribute("currentPageNo", currentPageNo);
		 if(null == getSessionAtrribute("xgCurrentPageNo")){
			 setSessionAtrribute("xgCurrentPageNo", currPageNo);
		 }
		 
		 
			Integer userId = Integer.valueOf(0);
			if(null != idString && !idString.equals("")){
				userId = Integer.valueOf(idString);
				userInfo = userInfoService.findUserInfoById(userId);  //查询用户的一条记录
				setSessionAtrribute("xgUid",userId);	 //用于在修改页面上传图片失败时，返回修改页面时需要用户ID
			}else{
				Integer uid = Integer.valueOf(getSessionAtrribute("xgUid").toString());
				userInfo = userInfoService.findUserInfoById(uid); 

			}
		
 
		//获取所有的公司名称
		List<Company> comList = companyService.findByCompany();
		
		company_id = userInfo.getCompany().getId(); //前台页面绑定显示
		 //根据公司名称查询
//		 Company company = companyService.findCompanyByName(null,companyID);
//		 String comName = company.getCompanyName();
		
		String chengshi = "";
		String  quyu = "";
		 //通过省份ID查询获取名称
		 Area areaProvince = areaService.findById(Integer.valueOf(userInfo.getProvince()));
		 if(null != userInfo.getCity() && !userInfo.getCity().equals("")){
			 Area areaCity = areaService.findById(Integer.valueOf(userInfo.getCity()));
			 chengshi = areaCity.getName(); //获取城市
		 }
		 if(null != userInfo.getZone() && !userInfo.getZone().equals("")){
			 Area areaZone = areaService.findById(Integer.valueOf(userInfo.getZone()));
			 quyu = areaZone.getName();  //获取区县
		 }
		 
		// 所在地
		// 获取省列表,在前台显示省份，页面获取值<*>
		provinces = areaService.findAreaByParendIdAndlvType(Area.NO_PARENT,
				Area.AreaType.PROVINCE);
		String shengfen = areaProvince.getName(); //获取省份
		
		
		// 查询省份的一条记录
		List<Area> sfList = areaService.findAreaByNameTypeid(shengfen,Area.AreaType.PROVINCE); 
		for (int i = 0; i < sfList.size(); i++) {
			Area sf_area = sfList.get(i);
			sf_id = sf_area.getId(); // 得到省份ID<*>
			// 通过省份ID查询所有城市，页面获取值<*>
			cityList = areaService.findAreaByParendIdAndlvType(sf_id,Area.AreaType.CITY);
			   // 查询城市的一条记录
				if(!chengshi.equals("")){
					List<Area> csList = areaService.findAreaByName(chengshi, sf_id);
					for (int j = 0; j < csList.size(); j++){
						Area cs_area = csList.get(j);
						cs_id = cs_area.getId(); // 得到城市ID<*>

						// 通过城市ID查询所有的区域，页面获取值<*>
						areaList = areaService.findAreaByParendIdAndlvType(cs_id, Area.AreaType.AREA);
						
						// 查询区域的一条记录
						if(!quyu.equals("")){
							List<Area> qyList = areaService.findAreaByName(quyu, cs_id); 
							for (int k = 0; k < qyList.size(); k++) {
								Area qy_area = qyList.get(j);
								qy_id = qy_area.getId(); // 得到区域ID<*>
							}
						}
						
					}
				}else{
					areaList = new ArrayList<Area>();
				}
				 

		}
		
		// 在把新user放入session中
		if(null != getSessionAtrribute("xgUserInfo")){
			removeSession("xgUserInfo");
		}
		setRequestAtrribute("xgUserInfo", userInfo);
		setRequestAtrribute("companyList", comList);
		if(null == getSessionAtrribute("xgUid")){
			setSessionAtrribute("xgUid",userId); //用户ID,修改页面上传文件时出错返回到当前修改页面要使用UID查询用户记录
		}
		
		return "enterUserModPage";
	}
	
	/**
	 * ljp 20120110  后台管理-修改用户信息
	 */
	public String dobsModUserInfo(){
//		UserInfo chagUser = getSessionUser();

		if(null == this.getSessionAtrribute(Constants.MAN_SESSION_USER))
		{
			return "lg";
		}
		Integer uid = Integer.valueOf(getSessionAtrribute("xgUid").toString());
		UserInfo chagUser = userInfoService.findUserInfoById(uid); 
		chagUser.setProvince(user.getProvince());  //省份
		chagUser.setCity(user.getCity());  //城市
		chagUser.setZone(user.getZone());  //区县
		Integer companyID = user.getCompany().getId();  //得到公司ID
		
		if(chagUser.getCompany().getId().intValue() != companyID.intValue()){
			//根据公司名称查询
			 Company company = companyService.findCompanyByName(null,companyID);
			 chagUser.setCompany(company);  //公司
		}
		if(null != user.getAccount() && !user.getAccount().equals("")){
			chagUser.setAccount(user.getAccount()); // 手机帐号
		}
		if(null != user.getPassword() && !user.getPassword().equals("")){
			chagUser.setPassword(user.getPassword()); // 密码
		}
		
		if(null != user.getUserName() && !user.getUserName().equals("")){
			chagUser.setUserName(user.getUserName()); // 姓名
		}
		
		chagUser.setType(user.getType());  //用户类型
		
		if(null != user.getEmail() && !user.getEmail().equals("")){
			chagUser.setEmail(user.getEmail()); // email
		}else{
			chagUser.setEmail(""); // email
		}
		if(null != user.getPwdQuestion() && !user.getPwdQuestion().equals("")){ //密码问题
			chagUser.setPwdQuestion(user.getPwdQuestion());
		}else{
			chagUser.setPwdQuestion("");
		}
		if(null != user.getPwdAnswer() && !user.getPwdAnswer().equals("")){ //问题答案
			chagUser.setPwdAnswer(user.getPwdAnswer());
		}else{
			chagUser.setPwdAnswer(user.getPwdAnswer());
		}	
		
		if(null != user.getQq() && !user.getQq().equals("")){
			chagUser.setQq(user.getQq());       //QQ号码
		}else{
			chagUser.setQq("");       //QQ号码
		}
		if(null != user.getPersonalHonepage() && !user.getPersonalHonepage().equals("")){
			chagUser.setPersonalHonepage(user.getPersonalHonepage());   //个人主页
		}else {
			chagUser.setPersonalHonepage("");   //个人主页
		}
		if(null != user.getPersonalIntro() && !user.getPersonalIntro().equals("")){
			chagUser.setPersonalIntro(user.getPersonalIntro());   //个人介绍
		}else {
			chagUser.setPersonalIntro("");   //个人介绍
		}
		
		if(null != getSessionAtrribute("htImgIcoPath")){
			 //设置图片路径在页面获取(backStageManage/userInfoAdd.jsp)
			 String personalIco = (String)getSessionAtrribute("htImgIcoPath");
			 if(null != personalIco && !personalIco.equals("")){
				 chagUser.setPersonalIco(personalIco);  //图片路径
			 }
		 }
		
		// 修改个人用户信息
		boolean modifyResult = userInfoService.modifyUserInfo(chagUser);
		if (!modifyResult) {
			actionError = "服务器异常，请联系管理员!";

			return "modifyInfoResult";
		} else {
			// 先清除session中user
		//	removeSession(Constants.SESSION_USER);
			if(null != getSessionAtrribute("htImgIcoPath")){
				removeSession("htImgIcoPath");
			}
			if(null != getSessionAtrribute("xgUid")){ //修改成功清除
				removeSession("xgUid");	 //用于在修改页面上传图片失败时，返回修改页面时需要用户ID
			}
			// 在把新user放入session中
		//	setSessionAtrribute("xgUserInfo", chagUser);
		//	setSessionAtrribute(Constants.SESSION_USER, chagUser);
//			if(null != getSessionAtrribute("xgUserInfo")){
//				removeSession("xgUserInfo");
//			}
			
			actionError = "用户基本信息修改成功!";
			return "modifyInfoResult";
		}

	}
	
	/**
	 *20120203  删除用户记录
	 */
	public String doDelUserInfoPage()throws Exception{
		if(null==this.getSessionAtrribute(Constants.MAN_SESSION_USER))
		{
		   return "lg";
		}
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String idString = request.getParameter("userId");  //获取(userInfoSearch.jsp页面)请求参数
		Integer userId = Integer.valueOf(0);
		if(null != idString && !idString.equals("")){
			userId = Integer.valueOf(idString);
		}
//		if(null != getSessionAtrribute("userId")){
//			removeSession("userId");
//		}
		//获取当前页数，等到删除记录,返回到用户信息列表的当前页面(userInfoSearch.jsp)
		String data = request.getParameter("currentPageNo"); //获取(userInfoSearch.jsp页面)请求参数
		if(null != data && !data.equals("")){
			int currentPageNo = Integer.valueOf(data).intValue();
			request.setAttribute("currentPageNo", currentPageNo);
		}
		UserInfo userInfo = userInfoService.findById(userId);
		userInfo.setFlagdel(1);  //删除用户[1:删除]
		userInfoService.updateUserInfo(userInfo);
		actionError = "删除用户成功.";
		return toShowUserInfo();
	}
	
	/**20120220
	 * 进入批量导入用户的页面
	 * @return
	 */
	public String toBatchImportUserPage() {
		if(null == this.getSessionAtrribute(Constants.MAN_SESSION_USER))
		{
			return "lg";
		}
		
		return "batchImportPage";
	}
	/**20120220
	 * 进入批量导入用户数据
	 * @return
	 */
	public String doBatchImportUserData() {
		if(null == this.getSessionAtrribute(Constants.MAN_SESSION_USER))
		{
			return "lg";
		}
		
		// 构造 XSSFWorkbook 对象，xlsFile 传入文件路径
		Workbook xwb = null;
		try {
			if(excelPostfixName.equals("xls")){
				xwb = new HSSFWorkbook(new FileInputStream(xlsFile)); //读取2003Excel.xls文件
			}else{
				xwb = new XSSFWorkbook("D:/aa.xls"); //读取2007Excel.xlsx文件
			}
		} catch (Exception e) {
			e.printStackTrace();
//			try {
//				xwb = new HSSFWorkbook(new FileInputStream(xlsFile)); //读取2003Excel.xls文件
//			} catch (Exception e1) {
//				// TODO Auto-generated catch block
//				e1.printStackTrace();
//			}  
		}
		try {
			// 读取第一章表格内容    
			Sheet sheet = xwb.getSheetAt(0);    
			// 定义 row、cell    
			Row row;    
			String cell="";
			List<Map<Integer, String>> userList = new ArrayList();
			// 循环输出表格中的内容    
			for (int i = sheet.getFirstRowNum()+1; i < sheet.getPhysicalNumberOfRows(); i++) { //获取行数据    
			    row = sheet.getRow(i);
			    Map<Integer, String> map = new HashMap();
			    boolean flag =false;
			    int colCnt = sheet.getRow(0).getPhysicalNumberOfCells(); //获取第一行头的标题有多少列
			   // System.out.println("Row行数：="+sheet.getPhysicalNumberOfRows());
			   // System.out.println("Cell列数：="+colCnt);
			    //如果某一行的第一列和第二列为空，就认为这行没有记录
			    if (((row.getCell(0) ==null || StringUtil.isEmpty(row.getCell(0).toString()))  && (row.getCell(1) ==null || StringUtil.isEmpty(row.getCell(1).toString())))) {
			    	continue;
			    }
			    
			    for (int j = row.getFirstCellNum(); j < colCnt; j++) { //获取列数据
			        // 通过 row.getCell(j).toString() 获取单元格内容，
			    	if (row.getCell(j) == null) {
			    		map.put(j, "");
			    	//	System.out.println("null...");
			    	}else{
			    		if ((j == 1 || j == 2 || j == 4 || j == 11) && (row.getCell(j).getCellType() ==0)) {
			    			DecimalFormat dFormat = new DecimalFormat("####");
			    			cell = dFormat.format(Double.parseDouble(row.getCell(j).toString()));
			    				//row.getCell(j).toString();
			    		//	System.out.println("第"+i+"行,第"+j+" 列的类型："+row.getCell(j).getCellType());
			    		} else {
			    			cell = row.getCell(j).toString();
			    		}
			    		
					     System.out.println("第"+i+"行,第"+j+"列的值="+cell);
					     map.put(j, cell);
			    	}
			       
			       // System.out.print(cell + "/t");    
			    }
			    if (!(StringUtil.isEmpty(map.get(0))  && StringUtil.isEmpty(map.get(1)))) {
			    	userList.add(map);//获取一行所有的列的数据放到list里
			    }
			}

			
			List<UserInfo> userListTran = transfer(userList);
			if(null != userListTran && userListTran.size()>0){
				 
				for(UserInfo user: userListTran){
					Date date = new Date();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
					date = sdf.parse(sdf.format(date));
					user.setAddDate(date); //添加的时间
					user.setRecentlyLoginDate(date); //最近登录时间
					
					userInfoService.userRegisterInfo(user); //操作数据库
				}
				actionError = "用户数据已导入.";
			}else{
				actionError = "Excel文件的用户数据不合格.";
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}    
		
		
		return "batchImportSuccess";
	}
	
	/**
	 * 该方法用来转换Map存放userinfo记录里面数据
	 * @param list
	 * @return
	 */
	private List<UserInfo> transfer(List<Map<Integer, String>> list) {
		List<UserInfo> ulist= new ArrayList<UserInfo>();
		List<Map<Integer, String>> listCompare = new ArrayList<Map<Integer,String>>();
		if(null != list){
			listCompare = list;
		}
		for (int i = 0; i < list.size(); i++) {
			UserInfo userInfo = new UserInfo();
			StringBuffer sbu = new StringBuffer();
			
			Map map = (Map)list.get(i); //获取第一条记录的数据
			
			if(null != map.get(0) && !map.get(0).equals("")){
				String keyVal0 = map.get(0).toString(); //获取用户名
				userInfo.setUserName(keyVal0);
			}else{
				sbu.append("用户名不能为空 ;");
			}
			boolean flag = false;
			if(listCompare.size() > 1){ //文件如果有两条记录以上就可以比较
				for (int j = 1; j < listCompare.size(); j++) { //从第2条记录比较
					Map mapCompare = (Map)listCompare.get(j); //获取第一条记录的数据
					if(i != j){ //不和本身记录比较
						if((null != map.get(1) && !map.get(1).equals("")) && (null != mapCompare.get(1) && !mapCompare.get(1).equals(""))){
							String account = mapCompare.get(1).toString(); //手机号码
							String moblieNo = map.get(1).toString(); //手机号码
							if(moblieNo.equals(account)){
								sbu.append("第"+(i+2)+"行手机号与第"+(j+2)+"行手机号重复;");
								this.addActionError("第"+(i+2)+"行："+sbu.toString());
								flag = true;
								
							}
						}
					}
					
				}
			}
			if(flag == true){ //如果有重复号码就跳出for循环
				continue;
			}
			
			if(null != map.get(1) && !map.get(1).equals("")){
				String keyVal1 = map.get(1).toString(); //帐号
				if(StringUtil.isAccount(keyVal1)){ //判断是否是手机号码
					String moblieNo = userInfoService.checkUserMobile(keyVal1); //检查手机号是否重复
					if(moblieNo.equals("")){
						userInfo.setAccount(keyVal1);
					}else{
						sbu.append("手机号码与数据库中重复;");
					}
				}else {
					sbu.append("输入的手机号码有误;");
				}
				
			}else{
				sbu.append("手机号码不能为空;");
			}
			
			if(null != map.get(2) && !map.get(2).equals("")){
				String keyVal2 = map.get(2).toString(); //密码
				userInfo.setPassword(keyVal2);
			}else{
				sbu.append("密码不能为空;");
			}
			
			
			if(null != map.get(3) && !map.get(3).equals("")){
				String keyVal3 = map.get(3).toString(); //密码查询问题
				userInfo.setPwdQuestion(keyVal3);
			}else{
				sbu.append("密码查询问题不能为空;");
			}
			
			if(null != map.get(4) && !map.get(4).equals("")){
				String keyVal4 = map.get(4).toString(); //密码查询答案
				userInfo.setPwdAnswer(keyVal4);
			}else{
				sbu.append("密码查询答案不能为空;");
			}
			
			if(null != map.get(5) && !map.get(5).equals("")){
				String keyVal5 = map.get(5).toString(); //所属公司名称
				Company company = companyService.findCompanyByName(keyVal5,null);
				
				if(null != company){
					userInfo.setCompany(company);
				}else{
					sbu.append("公司名称不存在;");
				}
			}else{
				sbu.append("公司名称不能为空;");
			}
			
			if(null != map.get(6) && !map.get(6).equals("")){  //省份不为空时
				String keyVal6 = map.get(6).toString(); //所属省
				
				String shengfen = keyVal6; //省的名称
				// 查询省份的一条记录
				List<Area> sfList = areaService.findAreaByNameTypeid(shengfen,Area.AreaType.PROVINCE);
				if(null != sfList && sfList.size()>0){
					for (int s = 0; s < sfList.size(); s++) {
						Area sf_area = sfList.get(s);
						sf_id = sf_area.getId();  // 得到省份ID<*>
					}
					userInfo.setProvince(String.valueOf(sf_id));  //设置省份ID
					
					if(null != map.get(7) && !map.get(7).equals("")){  //城市不为空时
						String keyVal7 = map.get(7).toString(); //所属市
						
						String chengshi = keyVal7;  //城市的名称
						 // 查询城市的一条记录
						List<Area> csList = areaService.findAreaByName(chengshi, sf_id);
						if(null != csList && csList.size()>0){
							for (int c = 0; c < csList.size(); c++) {
								Area cs_area = csList.get(c);
								cs_id = cs_area.getId();  // 得到城市ID<*>
							}
							userInfo.setCity(String.valueOf(cs_id)); //设置城市ID
						}else{
							sbu.append("输入的城市有误;");
						}
						
						if(null != map.get(8) && !map.get(8).equals("")){ //区县不为空时
							String keyVal8 = map.get(8).toString(); //所属区
							String quyu = keyVal8;  //区县的名称
							// 查询区县的一条记录
							List<Area> qyList = areaService.findAreaByName(quyu, cs_id); 
							if(null != qyList && qyList.size()>0){
								for (int q = 0; q < qyList.size(); q++) {
									Area cs_area = qyList.get(q);
									cs_id = cs_area.getId();  // 得到城市ID<*>
								}
								userInfo.setZone(String.valueOf(cs_id)); //设置区县ID
							}else{
								sbu.append("输入的区县有误;");
							}
							
						}else{
							userInfo.setZone("");
						}
						
					}else{
						userInfo.setCity("");
						userInfo.setZone("");
					}
					
				}else{
					sbu.append("选择的省份有误;");
				}
				
			}else{
				sbu.append("省份不能为空;");
			}
			
			if(null != map.get(9) && !map.get(9).equals("")){
				String keyVal9 = map.get(9).toString(); //账号类别(超过120是vip),0表示普通,1表示vip
				if(keyVal9.equals("普通用户")){
					userInfo.setType(Integer.valueOf(0));
				}else{
					userInfo.setType(Integer.valueOf(1));
				}
			}else{
				sbu.append("用户类型不存在;");
			}
			
			if(null != map.get(10) && !map.get(10).equals("")){
				String keyVal10 = map.get(10).toString(); //邮箱
				if(StringUtil.isEmail(keyVal10)){
					userInfo.setEmail(keyVal10);
				}else{
					sbu.append("输入的E_Mail电子邮箱格式有误;");
				}
			}else{
				userInfo.setEmail("");
			}
			
			if(null != map.get(11) && !map.get(11).equals("")){
				String keyVal11 = map.get(11).toString(); //qq号码
				if(StringUtil.isQq(keyVal11)){
					userInfo.setQq(keyVal11);
				}else{
					sbu.append("输入的QQ号码有误;");
				}
			}else{
				userInfo.setQq("");
			}
			
			if(null != map.get(12) && !map.get(12).equals("")){
				String keyVal12 = map.get(12).toString(); //个人主页
				if(StringUtil.isPersonalHonepage(keyVal12)){
					userInfo.setPersonalHonepage(keyVal12);
				}else{
					sbu.append("个人主页格式有误;");
				}
			}else{
				userInfo.setPersonalHonepage("");
			}
			
			if(null != map.get(13) && !map.get(13).equals("")){
				String keyVal13 = map.get(13).toString(); //个人介绍
				if(keyVal13.length() > 1000){
					sbu.append("个人简介不能超过1000字符;");
				}else{
					userInfo.setPersonalIntro(keyVal13);
				}
			}else {
				userInfo.setPersonalIntro("");
			}	
				
			if(null != map.get(14) && !map.get(14).equals("")){
				String keyVal14 = map.get(14).toString(); //余额
				if(StringUtil.isBalance(keyVal14)){
					userInfo.setBalance(new BigDecimal(keyVal14));
				}else{
					sbu.append("输入的余额错误;");
				}
			}else{
				sbu.append("余额不能为空。");
			}
			if(!sbu.toString().equals("")){
				this.addActionError("第"+(i+2)+"行："+sbu.toString());
			}else{
				ulist.add(userInfo); //如果数据正确就添加到list里面
			}
			
		}
				
		return ulist;
	}
	
	
}
